package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody  QueryPageBean queryPageBean){
        try{
            PageResult result= menuService.findPage(queryPageBean);
            return new Result(true, MessageConstant.GET_MENULIST_SUCCESS,result);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENULIST_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(int id){
        try{
            Menu menu=menuService.findById(id);
            return new Result(true,MessageConstant.GET_MENULIST_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENUINFO_FAIL);
        }
    }

    @RequestMapping("/findParentId")
    public Result findParentId(){
        try{
            List<Map<String,Integer>> list =menuService.finParentId();
            return new Result(true,null,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,null);
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Menu menu){
        try{
            menuService.update(menu);
            return new Result(true,MessageConstant.UPDATE_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_MENU_FAIL);
        }

    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try{
            menuService.add(menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }

    }

    @RequestMapping("/delete")
    public Result delete(int id){
        try{
            return  menuService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }

    }

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Menu> menuList = menuService.findAll();
        if(menuList != null && menuList.size() > 0){
            Result result = new Result(true, MessageConstant.QUERY_MENU_SUCCESS);
            result.setData(menuList);
            return result;
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }
}
